package registration;

import greenteam.mylook.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

public class Registration_Step_0 extends Fragment implements View.OnClickListener, CompoundButton.OnCheckedChangeListener
{
    Button btn_profile_picture;
    ImageView img_profile_picture;
    Bitmap yourSelectedImage;
    boolean choosed_photo = false;

    CheckBox chk_agree;
    boolean accepted_agreements = false;

	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
	}

	// onActivityCreated
	@Override
	public void onActivityCreated(Bundle savedInstanceState) 
	{
		super.onActivityCreated(savedInstanceState);
	}

	// onCreateView
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
	{
		View layoutView = inflater.inflate(R.layout.registration_step_0, container, false);

        btn_profile_picture = (Button)layoutView.findViewById(R.id.btn_profile_picture);
        btn_profile_picture.setOnClickListener(this);

        img_profile_picture = (ImageView)layoutView.findViewById(R.id.img_profile_picture);

        chk_agree = (CheckBox)layoutView.findViewById(R.id.chk_agree);
        chk_agree.setOnCheckedChangeListener(this);

		return layoutView;
	}

    @Override
    public void onClick(View view)
    {
        //startActivityForResult -> Galleria -> onActivityResult -> estrarre immagine scelta
        startActivityForResult(new Intent(Intent.ACTION_PICK).setType("image/*"), 0);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent)
    {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        if(resultCode == Activity.RESULT_OK)
        {
            try
            {
                Cursor cursor = getActivity().getContentResolver().query
                (
                        imageReturnedIntent.getData(),
                        new String[]{MediaStore.Images.Media.DATA}, null, null, null
                );

                if(cursor!= null && cursor.moveToFirst())
                {
                    String filePath = cursor.getString
                    (
                            cursor.getColumnIndex(MediaStore.Images.Media.DATA)
                    );
                    cursor.close();

                    yourSelectedImage = BitmapFactory.decodeFile(filePath);
                    yourSelectedImage = Bitmap.createScaledBitmap(yourSelectedImage, 300, 300, true);

                    img_profile_picture.setImageBitmap(yourSelectedImage);
                    choosed_photo = true;
                }
                else
                    choosed_photo = false;
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
        else
            choosed_photo = false;
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean status)
    {
        accepted_agreements = status;
    }

    public boolean StepOk()
    {
        return choosed_photo && accepted_agreements;
    }
}
